package cn.kone.system.controller;


import cn.kone.base.utlis.R;
import cn.kone.system.annotation.PreAuthorize;
import cn.kone.system.domain.Employee;
import cn.kone.system.query.EmployeeQuery;
import cn.kone.system.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口类")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部员工")
    @PreAuthorize(name = "查询全部员工", sn = "employee:findAllEmployees",descs = "查询全部员工")
    public R<List<Employee>> findAllEmployees() {

        List<Employee> all = employeeService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工")
    @PreAuthorize(name = "查询单个员工", sn = "employee:get",descs = "查询单个员工")
    public R<Employee> findOneEmployees(@PathVariable("id") Long id) {
        Employee one = employeeService.findOne(id);
        return R.ok(one);
    }

    /**
     * 新增和修改员工
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增和修改员工")
    @PreAuthorize(name = "新增和修改员工", sn = "employee:addAndUpdateEmployee",descs = "新增和修改员工")
    public R<Void> addAndUpdateEmployee(@RequestBody Employee employee) {
        Long id = employee.getId();
        try {
            if (id == null) {
                employeeService.addEmployee(employee);
            } else {
                employeeService.updateEmployee(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个员工")
    @PreAuthorize(name = "删除单个员工", sn = "employee:deleteEmployees",descs = "删除单个员工")
    public R<Void> deleteEmployees(@PathVariable("id") Long id) {
        try {
            employeeService.deleteEmployee(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询员工")
    @PreAuthorize(name = "分页查询员工", sn = "employee:pageQuery",descs = "分页查询员工")
    public PageInfo<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery) {

        List<Employee> list = employeeService.pageQuery(employeeQuery);
        return new PageInfo<Employee>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除员工")
    @PreAuthorize(name = "批量删除员工", sn = "employee:batchDeleteEmployee",descs = "批量删除员工")
    public R<Void> batchDeleteEmployee(@RequestBody Long[] ids) {
        try {
            employeeService.batchDeleteEmployee(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

}
